﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodeTime.TestProject {
	[TestClass()]
	public class CodeTimerTest {
		[TestMethod()]
		public void ThreadSleepTest() {
			CodeTimer.Time("Thread Sleep", 1, () => { Thread.Sleep(1000); });
			CodeTimer.Time("Empty Method", 10000000, () => { });
		}

		[TestMethod()]
		public void SingleThreadGCTest() {
			Int32 iteration = 100 * 1000;

			String s = "";
			CodeTimer.Time("String Concat", iteration, () => { s += "a"; });

			StringBuilder sb = new StringBuilder();
			CodeTimer.Time("StringBuilder Concat", iteration, () => { sb.Append("a"); });
		}

		[TestMethod()]
		public void SingleThreadTimeTest() {
			CodeTimer.Initialize();
			CodeTimer.Time(
				"",
				10,
				() => {
					Int32 tempValue = new Random().Next(990, 1000);
					System.Threading.Thread.Sleep(tempValue);
				}
			);
		}

		[TestMethod()]
		public void MultiThreadTimeTest() {
			CodeTimer.Time(
				"MultiThread",
				3,
				2,
				() => {
					Int32 tempValue = new Random().Next(990, 1000);
					System.Threading.Thread.Sleep(tempValue);
				},
				Console.WriteLine
			);
		}

		[TestMethod()]
		public void MultiThreadGCTest() {
			Int32 iteration = 100 * 1000;

			String s = "";
			CodeTimer.Time(
				"String Concat",
				1,
				2,
				() => {
					for (Int32 i = 0; i < iteration; i++) { s += "a"; }
				},
				Console.WriteLine
			);

			StringBuilder sb = new StringBuilder();
			CodeTimer.Time(
				"StringBuilder Concat",
				1,
				2,
				() => {
					for (Int32 i = 0; i < iteration; i++) { sb.Append("a"); }					
				},
				Console.WriteLine
			);
		}
	}
}
